--
-- Author: yawei.zhu
-- Date: 2018-09-13 20:54:50
--
-- PvpChampionOtherView
--
local PvpChampionForm = import(".PvpChampionForm")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ChampionUI

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView, self)
	end)

	self._ui.Button_more:addClickEventListener(function()
		display.pushToast( L("lua_code_text_458") )
	end)

	self._championForm = PvpChampionForm.new(self._ui.form)
	self._championForm:onOperate(function(op, data)
		if op == "showTeamDetailInfo" then
			if self._teams == nil then return end
			for _, team in ipairs(self._teams) do
				if data == team.teamId then
					executeFunc(self._onOperate, "openTeamView", team.throneTeam)
					break
				end
			end
		else
			executeFunc(self._onOperate, op, data)
		end
	end)

	self._ui.ready:setVisible(false)
	self._ui.seek:setVisible(false)
	self._ui.search:setVisible(false)

	local championPanel = self._ui.first:getChildByName("Panel")
    championPanel:addClickEventListener(function(sender)
    	local endPos = sender:getTouchEndPosition()
    	-- dump(endPos)
    	local playArmature = championPanel:getChildByName("Node_1"):getChildByTag(100)
    	if playArmature and playArmature:containWorldPoint(endPos.x, endPos.y) then
    		UIHandler:showHeadMenu({online=true, attr=self._rightBaseInfo, pos = cc.p(300, endPos.y)})
    	end
    end)
end

-- 更新左边显示
function ClassRef:updateChampionCharacter()
	local championPanel = self._ui.first:getChildByName("Panel")

	local txtTitle = championPanel:getChildByName("tlt")
	local txtMode = championPanel:getChildByName("text_mode")
	local imageBg = championPanel:getChildByName("Image")
	local firstPanel = championPanel:getChildByName("Panel_first")
	local txtDesc = firstPanel:getChildByName("tlt")
	local txtName = firstPanel:getChildByName("name")

	txtTitle:setString("")
	txtMode:setString("")
	txtDesc:setString(L("champion_notice7"))
	firstPanel:setVisible(true)

	-- 标题 根据_selectDay查找配置
	local throneConfig = GD:queryThroneByDay(self._curThroneInfo.day)
	if throneConfig then
		txtTitle:setString( L(throneConfig.name) )
		txtMode:setString( L(throneConfig.des) )
		imageBg:loadTexture( Res.ThroneBgPath .. throneConfig.picture1 .. ".png")
	end

	local baseInfo = self._curThroneInfo.baseInfo
	if not baseInfo then 
		firstPanel:setVisible(false)
		return
	end
	txtName:setString(baseInfo.name)

	if self.isLoading then return end -- 正在加载
	self.isLoading = true
	local AvatarFactory = app:getClass("AvatarFactory")
	-- 加载完之后直接创建角色
	local function onCreateCharacter(params)
		if tolua.isnull(self) then return end
	    self.isLoading = false
	    local character = AvatarFactory.createHero(params)
	   	character:playAni({name = "mapidle"})
	    -- character:setScale(0.8)
	    local characterNode = championPanel:getChildByName("Node_1")
	    characterNode:removeAllChildren()
	    characterNode:addChild(character)
	    character:setTag(100)
	end
	
   	baseInfo.inBattle = false
   	baseInfo.mountID = 0
   	self._rightBaseInfo = baseInfo
   	
   	AvatarFactory.preloadHero(baseInfo, function()
   		onCreateCharacter(baseInfo)
   	end)

end

-- 更新当前队伍列表
function ClassRef:updateCurTeamList(teams, battleResults)
	-- 存在异步调用，可能该view已经被释放了
	if cc.isDead(self) or not self._championForm then
		return
	end
	self._teams = teams
	self._championForm:updateOtherDayTeams(teams, self._curThroneInfo.day, battleResults)
end


function ClassRef:updateView(throneInfo)
	self._curThroneInfo = throneInfo
	-- 更新左边冠军显示
	self:updateChampionCharacter()
	-- 拉取16强队伍信息
	self:updateCurTeamList()
	executeFunc(self._onOperate, "reqThroneKnockOutTeam", {throneInfo.day, handler(self, self.updateCurTeamList)})
end

return ClassRef
